package cn.ffcs.uec.outStaffAdjustPostApply.component;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.IdSpace;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zkplus.databind.BindingListModelList;
import org.zkoss.zul.Div;
import org.zkoss.zul.ListModelList;

import cn.ffcs.uec.outStaff.manager.UecOutStaffManager;
import cn.ffcs.uec.outStaff.model.UecOutStaffInfo;
import cn.ffcs.uec.outStaffAdjustPostApply.component.bean.OutStaffListboxExtBean;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.NumericUtil;
import cn.ffcs.uom.common.util.PubUtil;
import cn.ffcs.uom.common.vo.PageInfo;
import lombok.Getter;
import lombok.Setter;

/**
 * 
 * @Title:
 * @Description:
 * @author Wang Yiren
 * @版权 FFCS（C） 2017
 * @date 2018年6月28日
 */
@Controller
@Scope("prototype")
public class OutStaffListboxExt extends Div implements IdSpace {

	private static final long serialVersionUID = 2202739062303790639L;

	private OutStaffListboxExtBean bean = new OutStaffListboxExtBean();

	/**
	 * zul
	 */
	private static final String ZUL = "/pages_nmg/uec_outStaff/comp/outStaffInfoBandbox/outStaff_listbox_ext.zul";

	private UecOutStaffInfo uecOutStaffInfo;

	private UecOutStaffManager uecOutStaffManager = (UecOutStaffManager) ApplicationContextUtil
			.getBean("uecOutStaffManager");

	@Getter
	@Setter
	private IPortletInfoProvider portletInfoProvider;

	public OutStaffListboxExt() {
		// 1.绑定页面
		Executions.createComponents(ZUL, this, null);
		// 2.页面控件与java属性绑定
		Components.wireVariables(this, bean);
		// 3.绑定监听事件
		Components.addForwards(this, this, '$');
		//执行查询
		onQuery();
	}

	/**
	 * 触发选中事件
	 */
	public void onSelectRequest() throws Exception {
		if (bean.getOutStaffListbox().getSelectedCount() > 0) {
			uecOutStaffInfo = (UecOutStaffInfo) bean.getOutStaffListbox().getSelectedItem().getValue();
			Events.postEvent("onSelectUecOutStaffInfo", this, uecOutStaffInfo);
		} else {
			Events.postEvent("onSelectNoUecOutStaffInfo", this, null);
		}
	}

	public void onQuery() {
		uecOutStaffInfo = new UecOutStaffInfo();
		PubUtil.fillPoFromBean(bean, uecOutStaffInfo);
		this.queryOutStaffInfo();
	}

	/**
	 * 查询
	 */
	public void queryOutStaffInfo() {
		int activePage = bean.getOutStaffListboxPaging().getActivePage();
		int pageSize = bean.getOutStaffListboxPaging().getPageSize();
		PageInfo pageInfo = uecOutStaffManager.queryPageInfoByUecOutStaffInfo(uecOutStaffInfo, activePage + 1,
				pageSize);
		ListModelList dataList = new BindingListModelList(pageInfo.getDataList(), true);
		bean.getOutStaffListbox().setModel(dataList);
		bean.getOutStaffListboxPaging().setTotalSize(NumericUtil.nullToZero(pageInfo.getTotalCount()));
	}
	/**
	 * 重置
	 */
	public void onReset() {
		clearVariable();
		bean.getPsnCode().setValue(null);
		bean.getIdNum().setValue(null);
		bean.getPsnName().setValue(null);
		ListboxUtils.clearListbox(this.bean.getOutStaffListbox());
		this.bean.getOutStaffListboxPaging().setTotalSize(1);
	}
	

	/**
	 * 清除变量
	 */
	public void clearVariable() {
		uecOutStaffInfo = null;
	}

	/**
	 * 翻页
	 */
	public void onOutStaffListboxPaging() {
		this.queryOutStaffInfo();
	}

}
